package com.cqh.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqh.common.R;
import com.cqh.entity.Employee;
import com.cqh.service.EmployService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployController {
    @Autowired
    private EmployService employService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password=employee.getPassword();
        password =DigestUtils.md5DigestAsHex(password.getBytes());

        //
        LambdaQueryWrapper<Employee> lambdaQueryWrapper= Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp=employService.getOne(lambdaQueryWrapper);

        if (emp==null){
            return  R.error("登录失败");
        }
        if (!emp.getPassword().equals(password)){
            return  R.error("登录失败");
        }
        if (emp.getStatus()==0){
            return  R.error("登录失败");
        }
        request.getSession().setAttribute("employee",emp.getId());

        return R.success(emp);
    }
    @PostMapping("/logout")
    public  R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");

        return  R.success("退出成功");
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest httpServletRequest){
        log.info("新增员工，员工信息",employee.toString());

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        Long empId=(Long) httpServletRequest.getSession().getAttribute("employee");


            employService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public  R<Page> page(int page,int pageSize,String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        //分页构造器
        Page pageInfo=new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lambdaQueryWrapper.orderByDesc(Employee::getCreateTime);
        employService.page(pageInfo,lambdaQueryWrapper);
        //执行查询
        return  R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        Long employId=(Long) request.getSession().getAttribute("employee");
        log.info(employee.toString());
        employService.updateById(employee);
        return R.success("编辑成功");
    }
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable  Long id){
        log.info("根据Id查询员工信息");
        Employee employee=employService.getById(id);
        if (employee!=null){
            return  R.success(employee);
        }
        return  R.error("没有找到员工信息");

    }
}
